package cn.hfatec.shms.web.ss.ssCriteria.action;

import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.constants.SysContant;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.base.ShmsBaseAction;
import cn.hfatec.shms.cache.CodeInfoCache;
import cn.hfatec.shms.enums.CodeType;
import cn.hfatec.shms.web.ss.ssCriteria.model.SsCriteria;
import cn.hfatec.shms.web.ss.ssCriteria.model.SsCriteriaParams;
import cn.hfatec.shms.web.ss.ssCriteria.service.SsCriteriaService;
import cn.hfatec.shms.web.ss.ssCustomer.model.SsCustomerParams;
import cn.hfatec.shms.web.xc.xcTjmxb.model.XcTjmxbParams;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 *
 * @author hfatec codegen
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Controller
@RequestMapping("/ss/ssCriteria")
public class SsCriteriaAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "SsCriteriaService")
    private SsCriteriaService ssCriteriaService;


    /**
     *
     * 查询列表
     *
     */
    @RequestMapping("/list")
    public String list(SsCriteriaParams ssCriteriaParams, Page page, Model model) {

        Table table = ssCriteriaService.querySsCriteriaTable(ssCriteriaParams, page);
        model.addAttribute("resultList", CodeInfoCache.getCodeInfo(CodeType.RESULT_TYPE));
        model.addAttribute("yesORno", CodeInfoCache.getCodeInfo(CodeType.YES_OR_NO));
        model.addAttribute("ssCriteriasTable", table);
        model.addAttribute("ssCriteriaParams", ssCriteriaParams);
        return "/ss/ssCriteria/list";
    }

    /**
     *
     * 异步查询列表
     *
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(SsCriteriaParams ssCriteriaParams, Page page, Model model) {
        try {
            list(ssCriteriaParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/ss/ssCriteria/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }



    /**
     *
     * 查询列表
     *
     */
    @RequestMapping("/selectList")
    public String selectList(SsCriteriaParams ssCriteriaParams, Page page, Model model) {

        Table table = ssCriteriaService.queryResultTable(ssCriteriaParams, page);
        model.addAttribute("resultList", CodeInfoCache.getCodeInfo(CodeType.RESULT_TYPE));
        model.addAttribute("yesORno", CodeInfoCache.getCodeInfo(CodeType.YES_OR_NO));
        model.addAttribute("ssCriteriasTable", table);
        model.addAttribute("ssCriteriaParams", ssCriteriaParams);
        return "/ss/ssCriteria/selectList";
    }
    /**
     * 跳转到选择产品权益页
     */
    @RequestMapping("/dorefreshCrux")
    @ResponseBody
    public String dorefreshCrux(SsCriteriaParams ssCriteriaParams, Page page, Model model) {

        try {
            selectList(ssCriteriaParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/ss/ssCriteria/selectList", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /*
         * 异步查询列表
         */
    @RequestMapping("/ajaxSelectList")
    @ResponseBody
    public String ajaxSelectList(SsCriteriaParams ssCriteriaParams, Model model, Page page) {
        try {
            selectList(ssCriteriaParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/ss/ssCriteria/selectTable", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 跳转到新增页
     *
     */
    @RequestMapping("/goAdd")
    @ResponseBody
    public String goAdd(Model model) {
        try {
            SsCriteria ssCriteria = new SsCriteria();
            model.addAttribute("resultList", CodeInfoCache.getCodeInfo(CodeType.RESULT_TYPE));
            model.addAttribute("yesORno", CodeInfoCache.getCodeInfo(CodeType.YES_OR_NO));
            model.addAttribute("ssCriteria", ssCriteria);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/ss/ssCriteria/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 跳转到编辑页
     *
     */
    @RequestMapping("/goEdit")
    @ResponseBody
    public String goEdit(Integer id, Model model) {

        try {
            SsCriteria ssCriteria = ssCriteriaService.getSsCriteria(id);
            model.addAttribute("resultList", CodeInfoCache.getCodeInfo(CodeType.RESULT_TYPE));
            model.addAttribute("ssCriteria", ssCriteria);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/ss/ssCriteria/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 根据IDs删除数据
     *
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteSsCriteria(String ids) {
        try {
            List<String> idList = new ArrayList<String>();
            if(ids.length() > 0) {
                idList = Arrays.asList(ids.split(","));
            }
            ssCriteriaService.deleteSsCriteriaBatch(idList);
        } catch(Exception e) {
            e.printStackTrace();
            logger.error("数据" + ids + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     *
     * 保存数据
     *
     */
    @RequestMapping("/save")
    @ResponseBody
    public String saveSsCriteria(@Valid SsCriteria ssCriteria, BindingResult result) {

        try {
            if(result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            ssCriteriaService.saveSsCriteria(ssCriteria);
        } catch(Exception e) {
            e.printStackTrace();
            logger.error("数据SsCriteria保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }


    /**
     * 导出excel
     */
    @RequestMapping("/export")
    @ResponseBody
    public String export(SsCriteriaParams ssCriteriaParams, HttpServletRequest request, HttpServletResponse response) {
        try {
            ssCriteriaService.exportExcel(ssCriteriaParams, request, response);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }
}
